//
//  CQTabBarController.swift
//  swift-weibo
//
//  Created by 陈乾 on 15/10/26.
//  Copyright © 2015年 陈乾. All rights reserved.
//

import UIKit

class CQTabBarController: UITabBarController {

//MARK: - viewDidLoad
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        //最简单的方式把文字颜色 改成和按钮的颜色一样
        tabBar.tintColor = UIColor.orangeColor()
        
        //添加子View
        let homeVC = CQHomeTableViewController()
        //函数重载
        addChildViewController(homeVC, image: "tabbar_home", seleImage: "tabbar_home_highlighted", title: "首页")
        
        let messageVC = CQDiscoverTableViewController()
        addChildViewController(messageVC, image: "tabbar_message_center", seleImage: "tabbar_message_center_highlighted", title: "消息")
        
        //添加中间的按钮的方法一：
        
        let plusVC = UIViewController()
        
        addChildViewController(plusVC, image: "f", seleImage: "f", title: "")
        
        
        let discoverVC = CQMessageTableViewController()
        addChildViewController(discoverVC, image: "tabbar_discover", seleImage: "tabbar_discover_highlighted", title: "发现")
        
        let profileVC = CQProflieTableViewController()
        addChildViewController(profileVC, image: "tabbar_profile", seleImage: "tabbar_profile_highlighted", title: "我TT")

    }
//MARK: - 重写父类的viewWillAppear
    override func viewWillAppear(animated: Bool)
    {
        //注意是传animated进去
        super.viewWillAppear(animated)
        //平均分配宽度
       let width = tabBar.bounds.width/CGFloat(5)
        //设置加号按钮到中间的位子
        plusButton.frame = CGRect(x: 2*width, y: 0, width: width, height: tabBar.bounds.height)
        //添加到tabBar
        tabBar.addSubview(plusButton)
        
    }
    
//MARK: - 添加子控制器  包装成nav
    private func addChildViewController(ViewController: UIViewController, image: String, seleImage: String, title: String)
    {
        ViewController.title = title
        ViewController.tabBarItem.image = UIImage(named: image)
        //原始的图片 不要体统渲染
        ViewController.tabBarItem.selectedImage = UIImage(named: seleImage)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //把控制器包装成导航控制器 并且添加到tabBarVC上面
        addChildViewController(UINavigationController(rootViewController: ViewController))
    }
//MARK: - 按钮点击事件
    func plusButtonClick()
    {
         !CQUserAccount.loginYN ? CQOauthViewController() : CQReleaseViewController()
        
       presentViewController(UINavigationController(rootViewController: CQReleaseViewController()), animated: true , completion: nil)
        
        
    }
    
    
//MARK: - 懒加载
    lazy var plusButton: UIButton = {
        
          //创建
          let btn = UIButton()
          //设置背景图片
          btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Normal)
          btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Highlighted)
          //设置图片
         btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
         btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        //添加点击事件
        btn.addTarget(self, action: "plusButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
          //返回
          return btn
    
    }()
    
    
    
}
